﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace VideoClipNameHelper
{
    public class CLogger
    {
        public static void Write(string msg, params object [] pp)
        {
            try
            {
                System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame(1, true);
                string className = sf.GetMethod().DeclaringType.Name;
                string methodName = sf.GetMethod().Name;
                //
                StreamWriter sw = new StreamWriter("__" + Path.GetFileNameWithoutExtension(System.Diagnostics.Process.GetCurrentProcess().ProcessName) +".txt", true);
                string ss = string.Format(msg, pp);
                sw.WriteLine(string.Format("{0:yy-M-d HH:mm}/{1}->{2}: {3}", DateTime.Now, className, methodName, ss.Trim()));
                sw.Flush();
                sw.Dispose();
            }
            catch { ;}
        }
        public static void Write(string msg, Exception e)
        {
            string s="";
            s += $"Message:{e.Message}{Environment.NewLine}";
            s += $"Source:{e.Source}{Environment.NewLine}";
            if (null != e.InnerException)
            {
                s += "{Environment.NewLine}";
                s += $".InnerException.Message:{e.InnerException.Message}{Environment.NewLine}";
                s += $"InnerException.Source:{e.InnerException.Source}{Environment.NewLine}";
            }
            s += "{Environment.NewLine}";
            try
            {
                System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame(1, true);
                string className = sf.GetMethod().DeclaringType.Name;
                string methodName = sf.GetMethod().Name;
                //
                StreamWriter sw = new StreamWriter("__" + Path.GetFileNameWithoutExtension(System.Diagnostics.Process.GetCurrentProcess().ProcessName) + ".txt", true);
                string ss = msg + ": " + s;
                sw.WriteLine(string.Format("{0:yy-M-d HH:mm}/{1}->{2}: {3}", DateTime.Now, className, methodName, ss.Trim()));
                sw.Flush();
                sw.Dispose();
            }
            catch {; }
        }
    }//class
}//namespace
